package com.sun.app.service.app;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import sun.core.json.JSONArray;
import sun.core.json.JSONObject;

import com.sun.app.service.BaseService;
import com.sun.util.AppResult;


public class CreateActivityService extends BaseService {
	
	private static final String GET_CITYID= "select CODE from m_city where CITYNAME = ?";
	private static final String GET_AREAID=	"select AREAID from c_area where AREANAME = ?";
	private static final String GET_TIPID = "select TIPID FROM c_tip where TIPNAME = ?";
	//活动信息插入
	private static final String ADD_ACTIVITY = "insert into c_activity(TITLE,TIPID,BEGINTIME,ENDTIME,CITYID,ACTMAPID,AREAID,STREET,ACTRANGE,STREETDETAIL,UID,SCOPE,PLAN,STATE,DETAIL,DATELINE)"+"" +
																		" values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,now())";
	//查询刚插入的ACTIVITY中的ACTID(确保ACRMAPID&&UID&&TITLE的唯一性)
	private static final String GET_ACTID = "select ACTID from c_activity where ACTMAPID = ? and UID = ? and TITLE = ?";
	
	//attention表插入
	private static final String ADD_ATTENTION= "insert into c_activity_attention(UID,ACTID,STATUS)"+"values(?,?,1)";
	//photo表插入
	private static final String ADD_ACTPHOTO = "insert into c_activity_photo(APPATH,ACTID,DATELINE)"+"values(?,?,now())";
	//创建临时圈子
	private static final String ADD_ROUND = "insert into c_round(ROUNDNAME,UID,STATUS,DETAIL,TOP,DATELINE,COUNT)"+"values(?,?,1,?,0,now(),1)";
	//查出活动圈子的ROUNDID
	private static final String GET_ROUNDID = "select ROUNDID from c_round where ROUNDNAME = ? and UID = ? and STATUS = ?";
	//将用户插入roundAttention表
	private static final String ADD_ROUND_ATTENTION = "insert into c_round_attention(UID,ROUNDID,ROLE,DATELINE)"+"values(?,?,2,now())";
	//将活动TIP插入圈子TIP中
	private static final String ADD_ROUND_TIP ="insert into c_round_tip(ROUNDID,TIPID)"+"values(?,?)";
	//圈子photo插入
	private static final String ADD_ROUND_PHOTO="insert into c_round_photo(RPATH,RID,DATELINE)"+"values(?,?,now())";
	
	public AppResult addActivity(HttpServletRequest request){
		
		System.out.println("add");
		System.out.println("11111"+request.getParameter("activity_days"));
		String userUID=(String) request.getSession().getAttribute("UID");
		AppResult r = new AppResult();
		
		int days = Integer.valueOf(request.getParameter("activity_days"));
		
		if(!(request.getParameter("activity_beginHour")!=null&&request.getParameter("activity_beginMin")!=null&&request.getParameter("activity_endHour")!=null&&request.getParameter("activity_endMin")!=null)){
			r.setResult("error");
			r.setStatus(300);
			System.out.println("时间未填写");
			return r;
		}
		Integer beginHour = Integer.valueOf(request.getParameter("activity_beginHour"));
		Integer beginMin = Integer.valueOf(request.getParameter("activity_beginMin"));
		Integer endHour =Integer.valueOf(request.getParameter("activity_endHour")); 
		Integer endMin =Integer.valueOf(request.getParameter("activity_endMin")); 
		//开始时间处理
			//设置开始时间的工具类
		Calendar calendar = Calendar.getInstance();
		int today = calendar.get(Calendar.DAY_OF_YEAR);
			//获取活动时间的天
		calendar.set(Calendar.DAY_OF_YEAR, today+days);
			//获取活动开始时间的点
		calendar.set(Calendar.HOUR_OF_DAY, beginHour);
			//获取活动开始时间的分钟
		calendar.set(Calendar.MINUTE, beginMin);
		//结束时间处理
			//设置结束时间的工具类
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.set(Calendar.DAY_OF_YEAR, today+days);
			//获取活动结束时间的点
		calendar.set(Calendar.HOUR_OF_DAY, endHour);
			//获取活动结束时间的分钟
		calendar.set(Calendar.MINUTE, endMin);
		
		String title=request.getParameter("activity_title");
		String provinceCode=request.getParameter("activity_province");
		String cityCode=request.getParameter("activity_city");
		//获取城市ID
		System.out.println("city"+cityCode);
		String area=request.getParameter("activity_area");
		//获取区域ID
		Integer areaId = jt.queryForInt(GET_AREAID,area);
		//如果没有图，插空图
		String actPhoto = "front/images/activity/demo/1.jpg";
		
		String actPointId=request.getParameter("activity_actPointId");
		String streetDetail=request.getParameter("activity_streetDetail");
		String range=request.getParameter("activity_range");
		String tip=request.getParameter("activity_tip");
		String detail=request.getParameter("activity_detail");
		System.out.println("test");
		String img1=request.getParameter("activity_img1");
		System.out.println("img1"+img1);
		String img2=request.getParameter("activity_img2");
		System.out.println("img2"+img2);
		String img3=request.getParameter("activity_img3");
		System.out.println("tip--->"+tip);
		Integer tipId = (Integer) jt.queryForList(GET_TIPID,tip).get(0).get("TIPID");
		System.out.println("begin"+calendar.getTime());
		System.out.println("end"+endCalendar.getTime());
		System.out.println("city"+cityCode);
		System.out.println("area"+areaId);
		System.out.println("tip"+tipId);
		System.out.println("UID"+userUID);
		
			//private static final String ADD_ACTIVITY = "insert into c_activity(TITLE,TIPID,BEGINTIME,ENDTIME,CITYID,AREAID,STREET,ACTRANGE,STREETDETAIL,UID,SOCPE,PLAN,STATE,DETAIL,DATELINE)"+"" +
	
		//" values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,now())";
		//将信息插入到activity表
		int update = jt.update(ADD_ACTIVITY,title,tipId,calendar.getTime(),endCalendar.getTime(),cityCode,actPointId,areaId,streetDetail,range,streetDetail,userUID,0,0,0,detail);
		//查询插入的活动中ACTID
		int actId = jt.queryForInt(GET_ACTID,actPointId,userUID,title);
		System.out.println("actId------->"+actId);
		//将用户插入attention表
		int updateAttention = jt.update(ADD_ATTENTION,userUID,actId);
		
		//判断插进去几个图
		if(img1=="null"){
			//无图片上传，先插入一条默认图
			int updatePhoto=jt.update(ADD_ACTPHOTO,actPhoto,actId);
		}
		else if(img2==null){
			int updatePhoto=jt.update(ADD_ACTPHOTO,img1,actId);
		}
		else if(img3==null){
			int updatePhoto1=jt.update(ADD_ACTPHOTO,img1,actId);
			int updatePhoto2=jt.update(ADD_ACTPHOTO,img2,actId);
		}
		else if(img1!=null&&img2!=null&&img3!=null){
			int updatePhoto1=jt.update(ADD_ACTPHOTO,img1,actId);
			int updatePhoto2=jt.update(ADD_ACTPHOTO,img2,actId);
			int updatePhoto3=jt.update(ADD_ACTPHOTO,img3,actId);
		}
		
		
		
		//创建一个临时圈子
		int updateRound = jt.update(ADD_ROUND,title,userUID,detail);
		//查询临时圈子的ID
		int roundId = jt.queryForInt(GET_ROUNDID,title,userUID,1);
		//将用户插入圈子attention表中
		int updateRoundAttention = jt.update(ADD_ROUND_ATTENTION,userUID,roundId);
		//将活动tip加入圈子TIP
		System.out.println(roundId);
		int updateRoundTip = jt.update(ADD_ROUND_TIP,roundId,tipId);
		
		
		//将活动photo加入
		if(img1==null){
			//无图片上传，先插入一条默认图
			int updateRoundPhoto = jt.update(ADD_ROUND_PHOTO,actPhoto,roundId);
		}
		else if(img2==null){
			int updateRoundPhoto1=jt.update(ADD_ROUND_PHOTO,img1,roundId);
		}
		else if(img3==null){
			int updateRoundPhoto1=jt.update(ADD_ROUND_PHOTO,img1,roundId);
			int updateRoundPhoto2=jt.update(ADD_ROUND_PHOTO,img2,roundId);
		}
		else if(img1!=null&&img2!=null&&img3!=null){
			int updateRoundPhoto1=jt.update(ADD_ROUND_PHOTO,img1,roundId);
			int updateRoundPhoto2=jt.update(ADD_ROUND_PHOTO,img2,roundId);
			int updateRoundPhoto3=jt.update(ADD_ROUND_PHOTO,img3,roundId);
		}
		
		r.setResult("success");
		r.setStatus(200);
		return r;
		
		
	}
	
	//查看同一地区同一人活动是否已存在,返回0是不存在，1是存在
	private static final String IS_EXIST = "select ACTID from c_activity where TITLE = ? and UID = ? and ACTMAPID = ?";
	public AppResult is_exist(HttpServletRequest request){
		AppResult r =new AppResult();
		String title=request.getParameter("activity_title");
		String actPointId=request.getParameter("activity_actPointId");
		//String userUID=(String) request.getSession().getAttribute("UID");
		String userUID="20002";
		List<Map<String, Object>> queryForList = jt.queryForList(IS_EXIST,title,userUID,actPointId);
		if(queryForList.size()==0){
			r.setStatus(200);
			r.setResult(0);
		}
		else{
			r.setStatus(200);
			r.setResult(1);
		}
		return r;
		
	}
	//得到好友头像及信息
	private static final String GET_FRIEND_LIST = "SELECT UID,FRIENDID FROM c_friend WHERE UID = ? AND STATE = 1";
	private static final String GET_FRIENDPHOTO = "select LIMG,MIMG,SIMG from c_user_hdphoto where UID = ?";
	private static final String GET_FRIEND_DETAIL = "select * from c_user where UID = ?";
	public AppResult showFriends(HttpServletRequest request){
		AppResult r = new AppResult();
		String UID = (String) request.getSession().getAttribute("UID");
		System.out.println("UID"+UID);
		List<Map<String, Object>> friendIdList = jt.queryForList(GET_FRIEND_LIST,UID);
		Iterator<Map<String, Object>> it = friendIdList.iterator();
		ArrayList allList = new ArrayList();
		while(it.hasNext()){
			Map next = it.next();
			String friendId = (String) next.get("FRIENDID");
			String friendPhoto = (String) jt.queryForMap(GET_FRIENDPHOTO,friendId).get("SIMG");
			String friendName = (String) jt.queryForMap(GET_FRIEND_DETAIL,friendId).get("USERTRUENAME");
			next.put("friendPhoto", friendPhoto);
			next.put("friendName", friendName);
			allList.add(next);
		}
		Map m = new HashMap();
		m.put("allList", allList);
		m.put("allListSize", allList.size());
		//System.out.println("friendIdList"+friendIdList.size());
		//System.out.println("allListSize"+m.get("allListSize"));
		r.setResult(m);
		r.setMsg("success");
		r.setStatus(200);
		return r;
	}
	
	private static final String SELECT_ROUNDS = "select ROUNDID from c_round_attention where UID = ?";
	private static final String SELECT_ROUND_PHOTO = "select RPATH from c_round_photo where RID = ? ";
	private static final String SELECT_ROUND_NAME = "select ROUNDNAME from c_round where ROUNDID = ? ";
	public AppResult showRounds(HttpServletRequest request){
		AppResult r = new AppResult();
		String UID = (String) request.getSession().getAttribute("UID");
		System.out.println("UID"+UID);
		List<Map<String, Object>> roundIdList = jt.queryForList(SELECT_ROUNDS,UID);
		Iterator<Map<String, Object>> it = roundIdList.iterator();
		ArrayList allList = new ArrayList();
		while(it.hasNext()){
			
			Map next = it.next();
			System.out.println("next------>"+next);
			Integer roundId = (Integer) next.get("ROUNDID");
			System.out.println("roundPhoto"+jt.queryForMap(SELECT_ROUND_PHOTO,roundId));
			String roundPhoto = (String) jt.queryForMap(SELECT_ROUND_PHOTO,roundId).get("RPATH");
			
			String roundName = (String) jt.queryForMap(SELECT_ROUND_NAME,roundId).get("ROUNDNAME");
			System.out.println("roundName"+roundName);
			next.put("roundPhoto", roundPhoto);
			next.put("roundName", roundName);
			allList.add(next);
		}
		Map m = new HashMap();
		m.put("allList", allList);
		m.put("allListSize", allList.size());
		System.out.println("allListSize"+m.get("allListSize"));
		r.setResult(m);
		r.setMsg("success");
		r.setStatus(200);
		return r;
	}
	
	
	
	//通过SchoolCode获得School的地图地址
	private static final String GET_SMAPIMAGE="select MAPIMAGES from c_map_city where SCHOOLID=?";
	public AppResult getSchoolMap(HttpServletRequest request){
		AppResult r = new AppResult();
		String schoolCode = request.getParameter("code");
		List<Map<String, Object>> MapImageList = jt.queryForList(GET_SMAPIMAGE,schoolCode);
		Map<String, Object> MapImage;
		if(MapImageList.size()==0){
			r.setStatus(300);
			r.setResult("error");
		}
		else{
			MapImage = MapImageList.get(0);
			String MapPath = (String) MapImage.get("MAPIMAGES");
			System.out.println("MapPath--->"+MapPath);
			r.setResult(MapPath);
			r.setStatus(200);
			System.out.println("r--->"+r);
		}
		return r;
	}
	//通过UID获得School的地图地址
	private static final String GET_SCHOOLID = "select SCHOOL from c_user where UID = ?";
	private static final String GET_SCHOOLNAME = "select SCHOOLNAME from m_school where SCHOOLID=?";
	public AppResult getUserSchoolMap(HttpServletRequest request){
		AppResult r = new AppResult();
		String UID = (String) request.getSession().getAttribute("UID");
		List<Map<String, Object>> schoolList = jt.queryForList(GET_SCHOOLID,UID);
		if(schoolList.size()==0){
		}
		else{
			String schoolCode = (String) schoolList.get(0).get("SCHOOL");
			List<Map<String, Object>> MapImageList = jt.queryForList(GET_SMAPIMAGE,schoolCode);
			String schoolName = (String) jt.queryForMap(GET_SCHOOLNAME,schoolCode).get("SCHOOLNAME");
			Map<String, Object> MapImage;
			if(MapImageList.size()==0){
				r.setStatus(300);
				r.setResult("error");
			}
			else{
				Map map = new HashMap();
				MapImage = MapImageList.get(0);
				String MapPath = (String) MapImage.get("MAPIMAGES");
				map.put("MapPath", MapPath);
				map.put("code", schoolCode);
				map.put("schoolName", schoolName);
				System.out.println("MapPath--->"+MapPath);
				r.setResult(map);
				r.setStatus(200);
				System.out.println("r--->"+r);
			}
		}
		return r;
	}
	
	//通过cityCode获得city的地图地址
	private static final String GET_MAPIMAGE="select MAPIMAGES from c_map_city where CITYID=?";
	public AppResult getCityMap(HttpServletRequest request){
		AppResult r = new AppResult();
		String cityCode = request.getParameter("cityCode");
		System.out.println(cityCode);
		List<Map<String, Object>> MapImageList = jt.queryForList(GET_MAPIMAGE,cityCode);
		Map<String, Object> MapImage;
		if(MapImageList.size()==0){
			r.setStatus(300);
			r.setResult("error");
		}
		else{
			MapImage = MapImageList.get(0);
			String MapPath = (String) MapImage.get("MAPIMAGES");
			System.out.println("MapPath--->"+MapPath);
			r.setResult(MapPath);
			r.setStatus(200);
			System.out.println("r--->"+r);
		}
		return r;
	}
	//通过用户id得到用户的城市
	private static final String GET_USERCITYID = "select CITY from c_user where UID = ?";
	private static final String GET_CITYNAME = "select CITYNAME from m_city where CODE = ?";
	
	public AppResult getUserCityMap(HttpServletRequest request){
		AppResult r = new AppResult();
		String UID = (String) request.getSession().getAttribute("UID");
		String cityCode = (String) jt.queryForMap(GET_USERCITYID,UID).get("CITY");
		String cityName = (String) jt.queryForMap(GET_CITYNAME,cityCode).get("CITYNAME");
		System.out.println(cityCode);
		List<Map<String, Object>> MapImageList = jt.queryForList(GET_MAPIMAGE,cityCode);
		Map<String, Object> MapImage;
		if(MapImageList.size()==0){
			r.setStatus(300);
			r.setResult("error");
		}
		else{
			Map map = new HashMap();
			MapImage = MapImageList.get(0);
			String MapPath = (String) MapImage.get("MAPIMAGES");
			map.put("MapPath", MapPath);
			map.put("cityName", cityName);
			map.put("cityCode", cityCode);
			System.out.println("MapPath--->111111111111111"+MapPath);
			r.setResult(map);
			r.setStatus(200);
			System.out.println("r--->"+r);
		}
		return r;
	}
	
	//通过cityCode获得city的mapid再获得该mapid上的所有点
	private static final String GET_ACTPOINT="select * from c_map_activity where MAPID=(select MAPID from c_map_city where CITYID = ? )";
	private static final String GET_SACTPOINT="select * from c_map_activity where MAPID=(select MAPID from c_map_city where SCHOOLID = ?)";
	private static final String GET_AREANAME="select AREANAME from c_area where AREAID=?";
	private static final String GET_TYPEIMAGE="select TYPEIMAGE from c_map_type where TYPE = ?";
	public AppResult getActivityPoint(HttpServletRequest request){
		AppResult r = new AppResult();
		String cityCode = request.getParameter("cityCode");
		List<Map<String, Object>> ActPointList = jt.queryForList(GET_ACTPOINT,cityCode);
		ListIterator it = ActPointList.listIterator();
		JSONArray pointArray = new JSONArray();
		while(it.hasNext()){
			Map map = (Map)it.next();
			JSONObject jo = new JSONObject();
			String areaName = (String) jt.queryForMap(GET_AREANAME, map.get("AREAID")).get("AREANAME");
			System.out.println("type--->"+map.get("TYPE"));
			String typeImage = (String)jt.queryForMap(GET_TYPEIMAGE,map.get("TYPE")).get("TYPEIMAGE");
			jo.put("areaName",areaName);
			jo.put("actPointId",map.get("ACTMAPID"));
			jo.put("mapX", map.get("MAPX"));
			jo.put("mapY", map.get("MAPY"));
			jo.put("type", map.get("TYPE"));
			jo.put("typeImage", typeImage);
			pointArray.add(jo);
		}
		System.out.println(pointArray.toString());
		r.setResult(pointArray);
		r.setStatus(200);
		r.setTotleCount(ActPointList.size());
		return r;
	}
	
	//得到学校活动的点
	public AppResult getSchoolActivityPoint(HttpServletRequest request){
		AppResult r = new AppResult();
		String schoolCode = request.getParameter("code");
		System.out.println(schoolCode);
		List<Map<String, Object>> ActPointList = jt.queryForList(GET_SACTPOINT,schoolCode);
		ListIterator it = ActPointList.listIterator();
		JSONArray pointArray = new JSONArray();
		System.out.println("AREAID~~~~~~~~~~~~~~~~`"+ActPointList.get(0).get("AREAID"));
		while(it.hasNext()){
			Map map = (Map)it.next();
			JSONObject jo = new JSONObject();
			String areaName = (String) jt.queryForMap(GET_AREANAME, map.get("AREAID")).get("AREANAME");
			System.out.println("type--->"+map.get("TYPE"));
			String typeImage = (String)jt.queryForMap(GET_TYPEIMAGE,map.get("TYPE")).get("TYPEIMAGE");
			jo.put("areaName",areaName);
			jo.put("actPointId",map.get("ACTMAPID"));
			jo.put("mapX", map.get("MAPX"));
			jo.put("mapY", map.get("MAPY"));
			jo.put("type", map.get("TYPE"));
			jo.put("typeImage", typeImage);
			pointArray.add(jo);
		}
		System.out.println(pointArray.toString());
		r.setResult(pointArray);
		r.setStatus(200);
		r.setTotleCount(ActPointList.size());
		return r;
	}
	
	@Override
	public Integer add(HttpServletRequest request, String ip) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer delete(HttpServletRequest request, String ip) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer update(HttpServletRequest request, String ip) {
		// TODO Auto-generated method stub
		return null;
	}
	
	
}
